Dead code is a computer programming term for code in the source code of a program which is executed but whose result is never used in any other computation.[1][2] The execution of dead code wastes computation time as its results are never used.
While the result of a dead computation may never be used the dead code may raise exceptions or affect some global state, thus removal of such code may change the output of the program and introduce unintended bugs. Compiler optimizations are typically conservative in their approach to dead code removal if there is any ambiguity as to whether removal of the dead code will affect the program output.
Contents |
int foo (int iX, int iY) { int iZ = iX/iY; return iX*iY; }
In the above example, although the division of iX by iY is computed and never used, it will throw an exception when a division by zero occurs. It is thus dead code and can be removed.
Dead code elimination is a form of compiler optimization in which dead code is removed from a program. Dead code analysis can be performed using live variable analysis, a form of static code analysis and data flow analysis. This is in contrast to unreachable code analysis which is based on control flow analysis.
The dead code elimination technique is in the same class of optimizations as unreachable code elimination and redundant code elimination.
In large programming projects, it is sometimes difficult to recognize and eliminate dead code, particularly when entire modules become dead. Test scaffolding can make it appear that the code is still live, and at times, contract language can require delivery of the code even when the code is no longer relevant.[3]